/*
	This class implements a producer. Each producer thread is associated with a unique Producer object.
*/
#ifndef PRODUCER_H_
#define PRODUCER_H_
#define PI 3.14
#include "TaskContainer.h"
#include "Config.h"
#include <list>
#include <math.h>
#include <time.h>
using namespace std;

/* a struct used for returning a pair of values from normal distribution */
typedef struct normalRandPair{
	double x;
	double y;
}NormalRandPair;

class Producer {
	TaskContainer* container;  //the container on which the producer is working
	Statistics* stat;  // producer's CAS statistics
	int peakLength;  //number of tasks that need to be produced in each burst
	int timeBetweenPeaks;  // idle time between bursts
	static bool stopFlag;	// a flag indicating that all producers should stop
	int taskId;  //number of tasks inserted by current producer
	list<long>* timeMeasurements;  // a list of the actual durations of the bursts
	struct timespec* ts;	// a struct used for time sampling
	void normalRand(NormalRandPair* nrp); // assign values of from normal distribution into the fields of nrp

public:
	/* producer identifier (not pthread_t thread id). needs to be explicitly assigned by caller */
	int id;

	Producer();
	void setContainer(TaskContainer* container){this->container = container;}
	void run();
	DummyTask* createTask(int id){return new DummyTask(id);}
	static void stop(){stopFlag = true;}
	virtual ~Producer();

	// getters
	int getPeakLength(){return peakLength;}
	list<long>* getTimeMeasurements(){return timeMeasurements;}
	Statistics* getStatistics(){return stat;}
};

#endif /* PRODUCER_H_ */
